﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessHeating
// 文件功能描述：采暖数据访问层
//
// 
// 创建标识：2012-07-27 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbAdapter.sqldbAdapter;
using System.Data;
using System.Data.SqlClient;
namespace DAO.WaiGaoQiao
{
    public class DataAccessHeating
    {
        public DataSet BindList(int start, int end, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY ItemID ASC) rownumber," +
                   "* from Gas_View where bDateFlag=0 " + strquery + ")B " +
                   "where B.rownumber > " + start.ToString() + " and B.rownumber < " + end.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        public DataSet GetListNew(int pagesize, int startindex, string strquery, out int count)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY Rdate DESC) rownumber,* " +
                               "from V_GAS_DATA_W " + strquery + ") B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                query = "select count(*) from V_GAS_DATA_W " + strquery;
                object obj = db.ExecuteScalar(conn, CommandType.Text, query);
                count = Convert.ToInt32(obj);
                return ds;
            }
        }

        public int SaveHeating(string[,] strs, string datetime, string DataFlag, string OperatorID, string macno, string starttime, string endtime)
        {
            SqlConnection conn = new SqlConnection(GetConntionString.conntionstring());
            SqlConnection myConn = conn;
            myConn.Open();
            SqlCommand myComm = new SqlCommand();
            SqlTransaction myTran;
            //创建一个事务
            myTran = myConn.BeginTransaction();
            string query = "";
            int count = 0;
            sqldb db = new sqldb();
            SqlConnection conns = new SqlConnection(GetConntionString.conntionstring());
            query = "select a.ID from Gas_Data a inner join Gas_Item b on a.ItemID=b.ItemID " +
                        "where Rdate='" + datetime + "' and a.dataflag=" + DataFlag + " and b.DataFlag=0 order by b.ItemID";
            DataSet ds1 = db.ExecuteDataset(conns, CommandType.Text, query);
            try
            {
                //从此开始，基于该连接的数据操作都被认为是事务的一部分
                //下面绑定连接和事务对象
                myComm.Connection = myConn;
                myComm.Transaction = myTran; //定位到pubs数据库

                //myComm.CommandText = query;

                for (int i = 0; i < strs.GetLength(0); i++)
                {
                    query = "update Gas_Data set OperatorID='" + OperatorID + "',MacNo='" + macno + "',StartTime='" + starttime + "',EndTime='" + endtime
                        + "',ItemID=" + strs[i, 12].ToString() + ",T1='" + strs[i, 0] + "',T2='" + strs[i, 1] + "',T3='" + strs[i, 2]
                        + "',T4='" + strs[i, 3] + "',T5='" + strs[i, 4] + "',T6='" + strs[i, 5] + "',T7='" + strs[i, 6]
                        + "',T8='" + strs[i, 7] + "',T9='" + strs[i, 8] + "',T10='" + strs[i, 9] + "',T11='" + strs[i, 10]
                        + "',T12='" + strs[i, 11] + "' where ID=" + ds1.Tables[0].Rows[i][0].ToString();
                    myComm.CommandText = query;
                    int rowcount = myComm.ExecuteNonQuery();//操作1
                    if (rowcount != 0)
                    {
                        count++;
                    }
                }
                //提交事务
                myTran.Commit();
                return count;
            }
            catch (Exception err)
            {
                throw new ApplicationException("事务操作出错，系统信息：" + err.Message);
            }
            finally
            {
                myConn.Close();
            }
        }
    }
}
